﻿using DJ.Dialog;
using DJ.ZEF.ThirdParty;
using NSJL.Biz.Background.Client;
using NSJL.DAL.DataModel.Entities;
using NSJL.Framework.Utils;
using NSJL.Plugin.Third.ThirdParty;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace DJ.Clients
{
    /// <summary>
    /// AuthInfoView.xaml 的交互逻辑
    /// </summary>
    public partial class AuthInfoView : Window
    {
        public string card { get; set; }
        public string QrCode { get; set; } = null;
        public AuthInfoView()
        {
            InitializeComponent();
            ComWindow.Start(this, Label1);
            VoiceHelper.GetInstance().Start(lb1.Content.ToString());
            tb1.Focus();
            DevicesInfo devicesInfo = biz.GetDevicesInfo("扫码").FirstOrDefault();
            if (devicesInfo != null)
            {
                BarCodeHelper.GetInstance().Start(devicesInfo.COM, Convert.ToInt32(devicesInfo.Baud), (a) =>
                {
                    ResponseMsg(a);
                }, Convert.ToInt32(devicesInfo.Type));

            }
            //var ini = new IniFile("Config/Config.ini");
            //var portName = ini.readKey("BarCodeConfig", "portName");
            //var baudRate = ini.readKey("BarCodeConfig", "baudRate").Trim().ToInt32();
            //var type = ini.readKey("BarCodeConfig", "type").Trim().ToInt32();
            //BarCodeHelper.GetInstance().Start(portName, baudRate, (a) =>
            //{
            //    ResponseMsg(a);
            //},type);
        }

        public void ResponseMsg(string a)
        {
            Dispatcher.Invoke(() =>
            {
                QrCode = a;
                DialogResult = true;
            });
        }


        private void btn_return(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            BarCodeHelper.GetInstance().Stop();
        }



        CommBiz biz = new CommBiz();
        //刷卡确定
        private void Button_OK(object sender, RoutedEventArgs e)
        {
           
        }


      

        private void tb1_GotFocus(object sender, RoutedEventArgs e)
        {
            VirtualKeyboardHelper v = new VirtualKeyboardHelper();
            v.CloseKeyboardWindow();
            v.HideInputPanel();
            v.CloseVirtualKeyboard();
        }



        // 声明 DispatcherTimer 用于延迟检测
        private DispatcherTimer _inputDelayTimer;
        private void tb1_TextChanged(object sender, TextChangedEventArgs e)
        {
            // 每次输入都重置计时器
            _inputDelayTimer.Stop();
            _inputDelayTimer.Start();
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // 初始化计时器，设置 300ms 间隔
            _inputDelayTimer = new DispatcherTimer
            {
                Interval = TimeSpan.FromMilliseconds(300)
            };

            // 计时器触发时执行的操作
            _inputDelayTimer.Tick += (s, ee) =>
            {
                _inputDelayTimer.Stop(); // 停止计时器
                HandleInputCompleted();  // 执行最终操作
            };
        }
        // 输入完成后的最终操作
        private void HandleInputCompleted()
        {
            string text = tb1.Text;
            if (text.Length >= 7)
            {
                if (text.Length == 7)
                {
                     text = text.PadLeft(7, '0');
                }
                Dispatcher.Invoke(() =>
                {
                    card = text;
                    DialogResult = true;
                });
            }
        }
    }
}
